//一般不改
//  NewImageViewController.m
//  NewChat
//
//  Created by 马欢 on 16/2/28.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "NewImageViewController.h"
#import "MyDataViewController.h"
#import "MyCenterViewController.h"

@interface NewImageViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)UIImageView *backgroundView;
@end

@implementation NewImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.tabBarController.tabBar.translucent = NO;
    //   背景图片
    self.backgroundView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.backgroundView.image = [UIImage imageNamed:@"100.jpg"];
    [self.view addSubview:self.backgroundView];
    [self.view sendSubviewToBack:self.backgroundView];

    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.title = @"选择头像";
    
#pragma mark === 布局===
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    //    设置分区，距离屏幕上下左右的间距
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 0, 20);//上左右下    //    设置滑动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.collectionView];
    //    设置代理
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
//    //注册cell
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    self.view.backgroundColor = [UIColor whiteColor];
    [self.tabBarController.tabBar setHidden:YES];
}

#pragma mark ==== dataSource 方法====
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 33;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
//设置cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    if (indexPath.row >= 0 || indexPath.row <30) {
        cell.layer.cornerRadius = cell.frame.size.width/2.0;
        NSString *imageName = [NSString stringWithFormat:@"%ld.jpg",indexPath.row+1];
        cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageName]];
        cell.backgroundView.layer.masksToBounds = YES;
        cell.backgroundView.layer.cornerRadius = cell.backgroundView.frame.size.width/2.0;
    }
    return cell;
}
//设置单元格大小
//一行装不下会自动在下一行显示
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(100, 100);
    
}
//cell点击事件
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [MyDataViewController sharedMyDataViewController].ImageBtn.backgroundColor = [UIColor clearColor];
    
    //    将新头像 加入数据库
    AVUser *user = [AVUser currentUser];
    NSString *imageName = [NSString stringWithFormat:@"%ld.jpg",indexPath.row+1];
    [user setObject:imageName forKey:@"userImageName"];
    [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            NSLog(@"改头像成功%@",user[@"userImageName"]);
        }
    }];
//    存入本地
    NSUserDefaults *user1 = [NSUserDefaults standardUserDefaults];
    //    存入数据
    [user1 setObject:imageName forKey:@"userImageName"];
    //    同步数据
    [user1 synchronize];
    [self.navigationController popViewControllerAnimated:YES];
}
@end
